﻿'******************************* File Header *********************************
'* File Name:    WmiParameterCollection.vb
'* Project:      WmiConnection
'* Copyright (c) Freddy Kuehne
'*
'* Default implementation for a wmi parameter collection.
'* 
'* This source is subject to the GNU Library General Public License (LGPL).
'* See http://wmiconnection.codeplex.com/license.
'* All other rights reserved.
'*****************************************************************************

''' <summary>
''' Default implementation for a wmi parameter collection.
''' </summary>
Public Class WmiParameterCollection
  Inherits DbParameterCollection
#Region "Variables"
  ''' <summary>
  ''' List of WmiParameters.
  ''' </summary>
  Private m_Parameter As New List(Of WmiParameter)
  ''' <summary>
  ''' Sync-Object.
  ''' </summary>
  Private m_SyncRoot As New Object
#End Region

#Region "DbParameterCollection"
  ''' <summary>
  ''' <see cref="DbParameterCollection.Add"/>
  ''' </summary>
  ''' <exception cref="ArgumentNullException">Exception occurs, when an empty <paramref name="value"/> is passed</exception>
  Public Overrides Function Add(value As Object) As Integer
    If (value Is Nothing) Then Throw New ArgumentNullException("value")
    m_Parameter.Add(DirectCast(value, WmiParameter))
    Add = m_Parameter.Count - 1
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.AddRange"/>
  ''' </summary>
  ''' <exception cref="ArgumentNullException">Exception occurs, when an empty <paramref name="values"/> is passed</exception>
  Public Overrides Sub AddRange(values As System.Array)
    If (values Is Nothing) Then Throw New ArgumentNullException("values")
    For Each obj In values
      Add(obj)
    Next
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.Clear"/>
  ''' </summary>
  Public Overrides Sub Clear()
    m_Parameter.Clear()
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.Contains"/>
  ''' </summary>
  Public Overrides Function Contains(value As Object) As Boolean
    Contains = (IndexOf(value) >= 0)
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.Contains"/>
  ''' </summary>
  Public Overrides Function Contains(value As String) As Boolean
    Contains = (IndexOf(value) >= 0)
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.CopyTo"/>
  ''' </summary>
  ''' <exception cref="ArgumentNullException">Exception occurs, when an empty <paramref name="array"/> is passed</exception>
  Public Overrides Sub CopyTo(array As System.Array, index As Integer)
    If (array Is Nothing) Then Throw New ArgumentNullException("array")
    For lv As Integer = 0 To Count - 1
      array.SetValue(Item(lv), index + lv)
    Next
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.Count"/>
  ''' </summary>
  Public Overrides ReadOnly Property Count As Integer
    Get
      Count = m_Parameter.Count
    End Get
  End Property

  ''' <summary>
  ''' <see cref="DbParameterCollection.GetEnumerator"/>
  ''' </summary>
  Public Overrides Function GetEnumerator() As IEnumerator
    GetEnumerator = m_Parameter.GetEnumerator
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.GetParameter"/>
  ''' </summary>
  Protected Overloads Overrides Function GetParameter(index As Integer) As System.Data.Common.DbParameter
    GetParameter = m_Parameter(index)
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.GetParameter"/>
  ''' </summary>
  Protected Overloads Overrides Function GetParameter(parameterName As String) As System.Data.Common.DbParameter
    GetParameter = GetParameter(IndexOf(parameterName))
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.IndexOf"/>
  ''' </summary>
  Public Overloads Overrides Function IndexOf(value As Object) As Integer
    IndexOf = m_Parameter.IndexOf(DirectCast(value, WmiParameter))
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.IndexOf"/>
  ''' </summary>
  Public Overloads Overrides Function IndexOf(parameterName As String) As Integer
    Dim param As WmiParameter

    IndexOf = -1
    For lv As Integer = 0 To Count - 1
      param = m_Parameter.Item(lv)
      If (param IsNot Nothing) AndAlso (String.Equals(parameterName, param.ParameterName, StringComparison.OrdinalIgnoreCase)) Then
        IndexOf = lv
        Exit For
      End If
    Next
  End Function

  ''' <summary>
  ''' <see cref="DbParameterCollection.Insert"/>
  ''' </summary>
  Public Overrides Sub Insert(index As Integer, value As Object)
    m_Parameter.Insert(index, DirectCast(value, WmiParameter))
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.IsFixedSize"/>
  ''' </summary>
  Public Overrides ReadOnly Property IsFixedSize As Boolean
    Get
      IsFixedSize = False
    End Get
  End Property

  ''' <summary>
  ''' <see cref="DbParameterCollection.IsReadOnly"/>
  ''' </summary>
  Public Overrides ReadOnly Property IsReadOnly As Boolean
    Get
      IsReadOnly = False
    End Get
  End Property

  ''' <summary>
  ''' <see cref="DbParameterCollection.IsSynchronized"/>
  ''' </summary>
  Public Overrides ReadOnly Property IsSynchronized As Boolean
    Get
      IsSynchronized = False
    End Get
  End Property

  ''' <summary>
  ''' <see cref="DbParameterCollection.Remove"/>
  ''' </summary>
  Public Overrides Sub Remove(value As Object)
    If (value IsNot Nothing) Then
      m_Parameter.Remove(DirectCast(value, WmiParameter))
    End If
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.RemoveAt"/>
  ''' </summary>
  Public Overloads Overrides Sub RemoveAt(index As Integer)
    m_Parameter.RemoveAt(index)
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.RemoveAt"/>
  ''' </summary>
  Public Overloads Overrides Sub RemoveAt(parameterName As String)
    Remove(Item(parameterName))
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.SetParameter"/>
  ''' </summary>
  Protected Overloads Overrides Sub SetParameter(index As Integer, value As DbParameter)
    If (index < Count) AndAlso (index >= 0) Then RemoveAt(index)
    Insert(index, value)
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.SetParameter"/>
  ''' </summary>
  Protected Overloads Overrides Sub SetParameter(parameterName As String, value As DbParameter)
    Dim index As Integer = IndexOf(parameterName)
    If (index >= 0) Then SetParameter(index, value)
  End Sub

  ''' <summary>
  ''' <see cref="DbParameterCollection.SyncRoot"/>
  ''' </summary>
  Public Overrides ReadOnly Property SyncRoot As Object
    Get
      SyncRoot = m_SyncRoot
    End Get
  End Property
#End Region
End Class
